package com.yida.system.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yida.system.api.domain.SysArea;
import com.yida.system.api.domain.SysRole;
import com.yida.system.api.domain.SysUser;
import com.yida.system.service.ISysAreaService;
import com.yida.system.service.ISysMenuService;
import com.yida.system.service.ISysPermissionService;
import com.yida.system.service.ISysRoleService;

/**
 * 用户权限处理
 * 
 * @author yida
 */
@Service
public class SysPermissionServiceImpl implements ISysPermissionService {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysAreaService areaService;

    /**
     * 获取角色数据权限
     * 
     * @param userId 用户Id
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     * 
     * @param userId 用户Id
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            List<SysRole> roles = user.getRoles();
            if (!roles.isEmpty() && roles.size() > 1) {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (SysRole role : roles) {
                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            } else {
                perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
            }
        }
        return perms;
    }

    /**
     * 获取地区数据权限
     * 
     * @param user 用户信息
     * @return
     */
    public String getAreaPermission(SysUser user) {
        StringBuffer sb = new StringBuffer();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            sb.append("0");
        } else {
            // 获取地区集合
            List<SysArea> areaList = areaService.selectSysAreaByUserId(user.getUserId());
            if (areaList != null && areaList.size() > 0) {
                areaList.stream().forEach(sa -> {
                    if (sa.getAreaId() != 1) {// 过滤掉泉州市
                        sb.append(sa.getAreaId()).append(",");
                    }
                });
            }
            if (sb.length() > 0) {
                // 删除末位的","
                sb.deleteCharAt(sb.length() - 1);
            }
        }
        return sb.toString();
    }
}
